package com.android.fluttercontainer;

import android.app.Application;

import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.plugin.common.MethodChannel;

import static com.android.fluttercontainer.MyMethodCall.METHOD_CHANNEL_KEY;

/**
 * 创建者 ：  huyangyang962
 * 创建日期： 2021/5/21 4:37 PM
 * 描述：
 */
public class FlutterApplication {

    private static volatile FlutterApplication sInstace = null;
    private static FlutterEngine flutterEngine;
    private static MethodChannel methodChannel;

    public static Application sApplication;

    public static void init(Application application) {
        sApplication = application;

        flutterEngine = new FlutterEngine(application);

        flutterEngine.getDartExecutor().executeDartEntrypoint(
                DartExecutor.DartEntrypoint.createDefault()
        );

        MethodChannel methodChannel = new MethodChannel(flutterEngine.getDartExecutor(), METHOD_CHANNEL_KEY);
        methodChannel.setMethodCallHandler(new MyMethodCall());

        FlutterEngineCache.getInstance().put("my_engine_id", flutterEngine);
    }


    public static void destory() {
        flutterEngine.destroy();
    }
}
